package com.itheima.service.impl;

import com.itheima.constant.MessageConstant;
import com.itheima.constant.StatusConstant;
import com.itheima.dto.SysUserLoginDTO;
import com.itheima.entity.SysUser;
import com.itheima.exception.AccountLockedException;
import com.itheima.exception.AccountNotFoundException;
import com.itheima.exception.PasswordErrorException;
import com.itheima.mapper.UserLoginMapper;
import com.itheima.service.UserLoginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


/**
 * @author iTaotao
 * @date 2023/7/26 1:00
 * @desciption: 用户登录接口实现类
 */
@Service
public class UserLoginServiceImpl implements UserLoginService {
    @Autowired
    private UserLoginMapper userLoginMapper;

    @Override
    public SysUser login(SysUserLoginDTO sysUserLoginDTO) {
        String loginName = sysUserLoginDTO.getLogin_name();
        String password = sysUserLoginDTO.getPassword();

        SysUser sysUser = userLoginMapper.seleteSysUserByLoginNameAndPassword(loginName);
        System.out.println(sysUser.getPassword());
        if (sysUser == null) {
            throw new AccountNotFoundException(MessageConstant.ACCOUNT_NOT_FOUND);

        }
       // String digestAsHex = DigestUtils.md5DigestAsHex(password.getBytes());
        if(!password.equals(sysUser.getPassword())){
            throw new PasswordErrorException(MessageConstant.PASSWORD_ERROR);
        }
        if (sysUser.getStatus() == StatusConstant.DISABLE) {
            //账号被锁定
            throw new AccountLockedException(MessageConstant.ACCOUNT_LOCKED);
        }
        return sysUser;


    }
}
